//
//  XQPublicIdentityPopView.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/6.
//

import UIKit

class XQPublicIdentityPopView: UIView {

    var sureHandle: (() -> Void)?
    
    var cancelHandle: (() -> Void)?
    
    private lazy var contentView : UIView = {
        let v = UIView()
        return v
    }()
    
    private lazy var bgImageView : UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "pop_base_bg")
        return imageView
    }()
    
    private lazy var sureBtn : UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("OK", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.backgroundColor = .mainTextColor
        btn.layer.cornerRadius = 24
        btn.addTarget(self, action: #selector(sureClick), for: .touchUpInside)
        return btn
    }()
    
    private lazy var calcelBtn : UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("Close", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.layer.cornerRadius = 24
        btn.layer.borderWidth = 1
        btn.layer.borderColor = UIColor.white.cgColor
        btn.addTarget(self, action: #selector(cancelClick), for: .touchUpInside)
        return btn
    }()
    
    private lazy var topImageView : UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "public_identity_top")
        return imageView
    }()
    
    private lazy var topLb : UILabel = {
        let lb = UILabel()
        lb.text = "Please follow the example to upload the front of the \n ID  card correctly"
        lb.font = UIFont.systemFont(ofSize: 12, weight: .medium)
        lb.textColor = .xq_black
        lb.numberOfLines = 0
        lb.textAlignment = .center
        return lb
    }()
    
    private lazy var errorLb : UILabel = {
        let lb = UILabel()
        lb.text = "Error sample"
        lb.font = UIFont.systemFont(ofSize: 12, weight: .medium)
        lb.textColor = .xq_black
        return lb
    }()
    
    private lazy var firstImageView : UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "identity_pop_first")
        return imageView
    }()
    
    private lazy var secondImageView : UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "identity_pop_second")
        return imageView
    }()
    
    private lazy var thirdImageView : UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "identity_pop_third")
        return imageView
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.isUserInteractionEnabled = true
        makeUI()
        makeConstraints()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        makeUI()
        makeConstraints()
    }
    
    private func makeUI() {
        self.addSubview(self.contentView)
        self.contentView.addSubview(self.bgImageView)
        self.contentView.addSubview(self.sureBtn)
        self.contentView.addSubview(self.calcelBtn)
        self.bgImageView.addSubview(self.topImageView)
        self.bgImageView.addSubview(self.topLb)
        self.bgImageView.addSubview(self.errorLb)
        self.bgImageView.addSubview(self.firstImageView)
        self.bgImageView.addSubview(self.secondImageView)
        self.bgImageView.addSubview(self.thirdImageView)
    }
    
    private func makeConstraints() {
        self.contentView.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        self.bgImageView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.width.equalTo(UIDevice.xq_width() - 32)
            make.height.equalTo(420)
        }
        self.sureBtn.snp.makeConstraints { make in
            make.top.equalTo(self.bgImageView.snp.bottom).offset(30)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(48)
        }
        self.calcelBtn.snp.makeConstraints { make in
            make.top.equalTo(self.sureBtn.snp.bottom).offset(15)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(48)
            make.bottom.equalTo(0)
        }
        self.topImageView.snp.makeConstraints { make in
            make.top.equalTo(40)
            make.left.equalTo(28)
            make.right.equalTo(-28)
        }
        self.topLb.snp.makeConstraints { make in
            make.top.equalTo(self.topImageView.snp.bottom).offset(58)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(34)
        }
        self.errorLb.snp.makeConstraints { make in
            make.top.equalTo(self.topLb.snp.bottom).offset(20)
            make.centerX.equalToSuperview()
        }
        
        let width = (UIDevice.xq_width() - 70) / 3
        
        self.firstImageView.snp.makeConstraints { make in
            make.width.equalTo(width)
            make.left.equalTo(15)
            make.height.equalTo(58)
            make.bottom.equalTo(-24)
        }
        self.secondImageView.snp.makeConstraints { make in
            make.width.equalTo(width)
            make.left.equalTo(self.firstImageView.snp.right).offset(4)
            make.height.equalTo(58)
            make.bottom.equalTo(-24)
        }
        self.thirdImageView.snp.makeConstraints { make in
            make.width.equalTo(width)
            make.left.equalTo(self.secondImageView.snp.right).offset(4)
            make.height.equalTo(74)
            make.centerY.equalTo(self.firstImageView)
        }
    }
    
    @objc private func sureClick() {
        sureHandle?()
    }
    
    @objc private func cancelClick() {
        cancelHandle?()
    }
}
